<?php
function func_response($str = '')
{
    echo $str;exit;
}
function func_get($str){
    return isset($_GET[$str]) ? $_GET[$str] : '';
}
function func_request($str){
    return isset($_REQUEST[$str]) ? $_REQUEST[$str] : '';
}
function func_post($str){
    return isset($_POST[$str]) ? $_POST[$str] : '';
}
require('./db.php');
session_start();
$u_id = $_SESSION['u_id'];
?>
<meta charset="utf-8" >
<meta http-equiv="content-type" content="text/html" >
<META HTTP-EQUIV="Pragma"   CONTENT="no-cache">
<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,width=device-width"   />
<meta name="format-detection" content="telephone=no" />
<meta name="app-mobile-web-app-capable"  content="yes" />
<meta name="app-mobile-web-app-status-bar-style" content="black-translucent" />
<style>
    .box{
        display: inline-block;
        border: 1px solid;
        padding: 10px;
        border-radius: 5px;
        vertical-align: top;
    }
    .edit_box{
        position: fixed;
        width: 100%;
        height: 100%;
        background: #cccccc80;
        top: 0;
        display: none;
    }
    .edit_box>div{
        margin: auto;
        text-align: center;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        top: 50%;
        background: #fff;
    }
</style>
<script>
    function postInfos(URL,postStr,functions,s){
        var xmlHttp=xmlInit();
        xmlHttp.open("POST", URL, true);
        xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
        xmlHttp.setRequestHeader("Content-TYPE","application/x-www-form-urlencoded");
        xmlHttp.send(postStr);
        xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                var pReturn="";
                pReturn=xmlHttp.responseText;
                functions(pReturn,s);
            }
        }

    }
    function xmlInit(){
        var xmlHttp = false;
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp =  new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                xmlHttp =  false;
            }
        }
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
            xmlHttp =  new XMLHttpRequest();
        }
        return xmlHttp;
    }
</script>
    <div id="login-box">
        <input id="t"  type="hidden" name="t" value='add_user' />
        <p>姓名: <input id="f_name" type="text" name="f_name" /></p>
        <p>账号: <input id="u_name" type="text" name="u_name" /></p>
        <p>密码: <input id="u_pwd" type="text" name="u_pwd" /></p>
        <input type="button" value="添加" onclick="submit()"/>
        <div>
            <textarea id="content" style="width: 80%;height: 100px;" placeholder="格式如：
管理员----admin----admin
张三----a123----a123"></textarea>
        </div>
        <input type="button" value="批量添加" onclick="p_submit()"/>
    </div>
    <script>
        function p_submit() {
            var content = document.getElementById("content");
            var url = "api.php";
            var params= "t=7&content=" + content.value
            postInfos(url,params,function (res) {
                if(res === '添加成功') {
                    alert(res);
                    window.location.reload();
                    return false;
                }
                alert(res);
            })
        }
        function submit(){
            var t = document.getElementById("t");
            var f_name = document.getElementById("f_name");
            var u_name = document.getElementById("u_name");
            var u_pwd = document.getElementById("u_pwd");
            if(f_name.value === '') {
                alert('账号不能为空');
                return false;
            }
            if(u_name.value === '') {
                alert('账号不能为空');
                return false;
            }
            if(u_pwd.value === '') {
                alert('密码不能为空');
                return false;
            }
            var url = "api.php";
            var params= "t=" + t.value + "&f_name=" + f_name.value + "&u_name=" + u_name.value + "&u_pwd=" + u_pwd.value
            postInfos(url,params,function (res) {
                if(res === '添加成功') {
                    alert(res);
                    window.location.reload();
                    return false;
                }
                alert(res);
            })
            return false;
        }
    </script>
    <?php
    $sql = "select id,is_admin from lyl_users where id=" . $u_id;
    $user = Database::query($sql);
    if ($user) {
        $is_admin = 0;
        if($user[0]['is_admin']) {
            $is_admin = 1;
        }
    } else {
        $_SESSION['u_id'] = null;
        header("Location: /");
        exit;
    }
    if(!$is_admin) {
        header("Location: /sing.php");
    }


    $p = func_request("p");
    $size = 20;
    if(!$p) {
        $p = 1;
    }
    $sql = "select count(id) c from lyl_users";
    $count_rs = Database::query($sql);
    if (!$count_rs) {
        $c = 0;
    } else {
        $c = $count_rs[0]['c'];
    }
    require('./page.php');
    $page = new Page(10,$c,3);
    $sql = "select * from lyl_users limit " .$page->offestPage.','.$page->colNum;
    $list = Database::query($sql);
    if (!$list) {
        $list = [];
    }
    ?>
    <script>
        function out() {
            var url = "api.php";
            var params= "t=out";
            postInfos(url,params,function (res) {
                alert(res);
                window.location.reload();
            })
        }
    </script>
<?php if($is_admin) { ?>
    <a href="/">已填入列表</a>
    <a href="/src/user.php">人员列表</a>
    <a href="/src/sing.php.php">信息列表</a>
<?php } ?>
<a href="javascript:;" onclick="out()">退出</a>

    <style>
        #hor-zebra
        {
            font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
            font-size: 12px;
            width: 100%;
            text-align: left;
            border-collapse: collapse;
        }
        #hor-zebra th
        {
            font-size: 14px;
            font-weight: normal;
            padding: 10px 8px;
            color: #039;
        }
        #hor-zebra td
        {
            padding: 8px;
            color: #669;
        }
        #hor-zebra .odd
        {
            background: #e8edff;
        }
    </style>
    <h2 style="text-align: center">人员列表</h2>
<script>
    function toAjax(url) {
        postInfos(url,'',function (res) {
            window.location.reload();
        });
    }
</script>
<div style="text-align: center;">
    <?=$page->show();?>
</div>
    <table id="hor-zebra" summary="Employee Pay Sheet">
        <thead>
        <tr>
            <th scope="col">id</th>
            <th scope="col">姓名</th>
            <th scope="col">账号</th>
            <th scope="col">密码</th>
            <th scope="col">时间</th>
            <th scope="col">操作</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($list as $item) { ?>
            <tr class="odd">
                <td><?=$item["id"] ?> </td>
                <td><?=$item["f_name"] ?></td>
                <td><?=$item["u_name"] ?></td>
                <td><?=$item["u_pwd"] ?></td>
                <td><?=$item["time"] ?></td>
                <td>
                    <?php if($is_admin) { ?>
                        <?php if($item["is_admin"] == 0) {?>
                            <?php if($item["use"] == 1) { ?>
                                <input type="button" value="隐藏" onclick="toAjax('./api.php?t=4&use=0&id=<?=$item["id"]?>')">
                            <?php } else { ?>
                                <input type="button" value="显示" onclick="toAjax('./api.php?t=4&use=1&id=<?=$item["id"]?>')">
                            <?php } ?>
                        <?php } ?>
                        <input type="button" value="编辑" onclick="edit('<?=$item["id"] ?>','<?=$item["f_name"] ?>','<?=$item["u_name"] ?>','<?=$item["u_pwd"] ?>')">

                        <input type="button" value="删除" onclick="del(<?=$item["id"] ?>)">
                    <?php } ?>
                </td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
<div class="edit_box" id="edit_box">
    <div class="box" >
        <form>
            <input id="e_t"  type="hidden" name="t" value='5' />
            <p>ID: <input id='e_id' type="text" name="id" disabled /></p>
            <p>姓名: <input id='e_f_name' type="text" name="f_name" /></p>
            <p>账号: <input id='e_u_name' type="text" name="u_name" /></p>
            <p>密码: <input id='e_u_pwd' type="text" name="u_pwd" /></p>
            <input type="button" value="确认编辑" onclick="e_submit()"/>
            <input type="button" value="关闭" onclick="e_close()"/>
        </form>
    </div>
</div>
<script>
    function e_submit(){
        var t = document.getElementById("e_t");
        var id = document.getElementById("e_id");
        var f_name = document.getElementById("e_f_name");
        var u_name = document.getElementById("e_u_name");
        var u_pwd = document.getElementById("e_u_pwd");
        if(f_name.value === '') {
            alert('账号不能为空');
            return false;
        }
        if(u_name.value === '') {
            alert('账号不能为空');
            return false;
        }
        if(u_pwd.value === '') {
            alert('密码不能为空');
            return false;
        }
        var url = "api.php";
        var params= "id="+ id.value +"&t=" + t.value + "&f_name=" + f_name.value + "&u_name=" + u_name.value + "&u_pwd=" + u_pwd.value
        postInfos(url,params,function (res) {
            if(res === '编辑成功') {
                alert(res);
                window.location.reload();
                return false;
            }
            alert(res);
        })
        return false;
    }
    function edit(id,f_name,u_name,u_pwd) {
        document.getElementById("e_id").value = id;
        document.getElementById("e_f_name").value = f_name;
        document.getElementById("e_u_name").value = u_name;
        document.getElementById("e_u_pwd").value = u_pwd;
        e_show();
    }
    function e_close() {
        document.getElementById('edit_box').style.display = 'none';
    }
    function e_show() {
        document.getElementById('edit_box').style.display = 'block';
    }
    function del(id) {
        if(confirm("是否确认删除？")) {
            var url = "api.php?t=6";
            var params = 'id=' + id;
            postInfos(url,params,function (res) {
                if(res === '成功') {
                    window.location.reload();
                    return false;
                }
                alert(res);
            })
        }

    }
</script>


